package controllers.customer;

import java.util.Collection;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import services.ActorService;
import services.GalleryService;
import services.PaintingService;
import controllers.AbstractController;
import domain.Gallery;
import domain.Painting;

@Controller
@RequestMapping("/gallery/customer")
public class GalleryCustomerController extends AbstractController {

	// Services
	// -----------------------------------------------------------------

	@Autowired
	GalleryService galleryService;
	@Autowired
	PaintingService paintingService;

	@Autowired
	ActorService actorService;

	// Constructors -----------------------------------------------------------

	public GalleryCustomerController() {
		super();
	}

	// Listing ---------------------------------------------------------------

	@RequestMapping("/list")
	public ModelAndView list(@RequestParam(required=false) String message) {
		ModelAndView result;
		Collection<Gallery> galleries;
		galleries = galleryService.findGalleriesByPrincipal();

		result = new ModelAndView("gallery/list");
		result.addObject("galleries", galleries);
		result.addObject("URI", "/gallery/customer/list.do");
		result.addObject("message", message);
		return result;
	}
	
	@RequestMapping("/listToChoose")
	public ModelAndView listToChoose(@RequestParam int paintingId) {
		ModelAndView result;
		Painting painting = paintingService.findOne(paintingId);
		Collection<Gallery> galleries;
		galleries = galleryService.findGalleriesByPrincipal();
		galleries.remove(painting.getGallery());

		result = new ModelAndView("gallery/list");
		result.addObject("galleries", galleries);
		result.addObject("painting", painting);
		result.addObject("URI", "/gallery/customer/listToChoose.do");
		return result;
	}

	// Creation ---------------------------------------------------------------
	
	@RequestMapping(value = "/create", method = RequestMethod.GET)
	public ModelAndView create() {
		ModelAndView result;

		Gallery gallery = galleryService.create();
		result = createEditModelAndView(gallery);

		return result;
	}

	// Edition ----------------------------------------------------------------

	@RequestMapping(value = "/save", method = RequestMethod.POST)
	public ModelAndView save(@Valid Gallery gallery, BindingResult bindingResult) {
		ModelAndView result;		

		if (bindingResult.hasErrors()) {
			result = createEditModelAndView(gallery);
		} else {
			try {
				galleryService.save(gallery);			
				result = new ModelAndView("redirect:/gallery/customer/list.do");
				result.addObject("message", "gallery.commit.success");
			} catch (Throwable oops) {
				result = createEditModelAndView(gallery, oops.getMessage());
			}
		}

		return result;
	}

	@RequestMapping(value = "/delete", method = RequestMethod.GET)
	public ModelAndView delete(int galleryId) {
		ModelAndView result;		
		Gallery gallery = galleryService.findOne(galleryId);
		
		try {
			galleryService.delete(gallery);	
			result = new ModelAndView("redirect:/gallery/customer/list.do");
			result.addObject("message", "gallery.delete.success");
		} catch (Throwable oops) {
			Collection<Gallery> galleries;
			galleries = galleryService.findGalleriesByPrincipal();
			result = new ModelAndView("gallery/list");
			result.addObject("galleries", galleries);
			result.addObject("message", oops.getMessage());
		}

		return result;
	}


	// Ancillary methods ------------------------------------------------------

	protected ModelAndView createEditModelAndView(Gallery gallery) {
		
		ModelAndView result;

		result = createEditModelAndView(gallery, null);

		return result;
	}

	protected ModelAndView createEditModelAndView(Gallery gallery, String message) {
		ModelAndView result;

		result = new ModelAndView("gallery/edit");
		result.addObject("gallery", gallery);
		result.addObject("message", message);

		return result;
	}	
	
}
